package com.sc.nft.controller;

import cn.hutool.core.util.StrUtil;
import com.sc.nft.annotation.UserLogin;
import com.sc.nft.controller.request.*;
import com.sc.nft.controller.vo.IsPledgeConditionsVO;
import com.sc.nft.entity.UserInfo;
import com.sc.nft.entity.vo.DaoPledgeDetailVO;
import com.sc.nft.entity.vo.DaoPledgePageVO;
import com.sc.nft.entity.vo.DaoPledgeRecordDetailVO;
import com.sc.nft.entity.vo.DaoPledgeRecordVO;
import com.sc.nft.enums.RedisKeyEnum;
import com.sc.nft.exception.GlobalRunTimeException;
import com.sc.nft.pool.UserPool;
import com.sc.nft.service.DaoPledgeService;
import com.sc.nft.sup.ErrorCode;
import com.sc.nft.sup.PageResult;
import com.sc.nft.sup.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.redisson.Redisson;
import org.redisson.api.RLock;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;


@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/daoPledge")
@Api(tags = "燕琅嬛质押")
public class DaoPledgeController {

    private final DaoPledgeService daoPledgeService;
    private final Redisson redisson;


    @ApiOperation(value = "质押活动列表", notes = "质押活动列表", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/pagePledge", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public PageResult<DaoPledgePageVO> pagePledge(@Valid @RequestBody PageDaoPledgeRequest request) {
        return PageResult.success(daoPledgeService.pagePledge(request.getDaoId(),request.getPageNo(), request.getPageSize()));
    }


    @ApiOperation(value = "质押活动详情", notes = "质押活动详情", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/detailPledge", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result<DaoPledgeDetailVO> detailPledge(@Valid @RequestBody IdRequest request) {
        return Result.success(daoPledgeService.detailPledge(request.getId(), UserPool.getUserId()));
    }


    @ApiOperation(value = "活动参与记录", notes = "活动参与记录", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/pagePledgeRecord", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public PageResult<DaoPledgeRecordVO> pagePledgeRecord(@Valid @RequestBody DaoPledgeRecordRequest request) {
        return PageResult.success(daoPledgeService.pagePledgeRecord(request.getDaoId(),request.getPageNo(), request.getPageSize(), UserPool.getUserId()));
    }


    @ApiOperation(value = "活动参与详情", notes = "活动参与详情", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/detailPledgeRecord", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result<DaoPledgeRecordDetailVO> detailPledgeRecord(@Valid @RequestBody IdRequest request) {
        return Result.success(daoPledgeService.detailPledgeRecord(UserPool.getUserId(), request.getId()));
    }


    @ApiOperation(value = "质押共创", notes = "质押共创", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/pledge", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result<String> pledge(@Valid @RequestBody DaoPledgeRequest request) {
        UserInfo user = UserPool.getUser();
        if (!StrUtil.equals(request.getDealPwd(), user.getTransactionPassword())) {
            return Result.fail(ErrorCode.PASSWORD_ERROR);
        }

        String key = StrUtil.format("{}{}", RedisKeyEnum.DAO_PLEDGE.getKey(), request.getPledgeId());
        RLock lock = redisson.getLock(key);
        try {
            boolean res = lock.tryLock();
            if (!res) {
                throw new GlobalRunTimeException("太火爆了,请稍后再试");
            }
            daoPledgeService.pledge(user.getId(), request.getDaoId(), request.getPledgeId(), request.getPeriodsId(), request.getCollections());
            return Result.success();
        } catch (GlobalRunTimeException e) {
            log.error("质押共创异常", e);
            throw e;
        } finally {
            if (lock.isLocked() && lock.isHeldByCurrentThread()) {
                lock.unlock();
            }
        }

    }

    @ApiOperation(value = "是否可以满足质押条件", notes = "是否可以满足质押条件", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/isPledgeConditions", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result<IsPledgeConditionsVO> isPledgeConditions(@Valid @RequestBody DaoPledgeCheckRequest request) {
        return Result.success(new IsPledgeConditionsVO(daoPledgeService.isPledgeConditions(UserPool.getUser().getId(), request.getDaoId(), request.getPledgeId(), request.getPeriodsId(), request.getCollections())));
    }
}
